package com.mutual.commom.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadXML {
	
	public static String getValue(String path) {
		File file = new File(path);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		NodeList ips = null;
		Document doc = null;
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.parse(file);
			ips = doc.getElementsByTagName("value");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ips.item(0).getFirstChild().getNodeValue();
	}
	
	public static String getIp(String path){
		File file = new File(path);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
	    DocumentBuilder builder = null;
	    NodeList ips = null;
	    Document doc = null;
		try {
			builder = factory.newDocumentBuilder();		
			doc = builder.parse(file);
		    ips = doc.getElementsByTagName("ipf"); 	    
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}  catch (IOException e) {
			e.printStackTrace();
		}
		return ips.item(0).getFirstChild().getNodeValue();
	}
	
	public static String getFile(String path){
		File file = new File(path);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
	    DocumentBuilder builder = null;
	    NodeList files = null;
	    Document doc = null;
		try {
			builder = factory.newDocumentBuilder();		
			doc = builder.parse(file);
		    files = doc.getElementsByTagName("file"); 	    
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}  catch (IOException e) {
			e.printStackTrace();
		}
		return files.item(0).getFirstChild().getNodeValue();
	}

	public static String getPath(String path){
		File file = new File(path);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
	    DocumentBuilder builder = null;
	    NodeList files = null;
	    Document doc = null;
		try {
			builder = factory.newDocumentBuilder();		
			doc = builder.parse(file);
		    files = doc.getElementsByTagName("path"); 	    
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}  catch (IOException e) {
			e.printStackTrace();
		}
		return files.item(0).getFirstChild().getNodeValue();
	}
	public static String getQR(String path){
		File file = new File(path);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
	    DocumentBuilder builder = null;
	    NodeList qrs = null;
	    Document doc = null;
	    String qr ="";
		try {
			builder = factory.newDocumentBuilder();		
			doc = builder.parse(file);
		    qrs = doc.getElementsByTagName("p"); 
		    for(int i = 0; i < qrs.getLength(); i ++){
		    	qr += qrs.item(i).getFirstChild().getNodeValue()+":";
		    }
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}  catch (IOException e) {
			e.printStackTrace();
		}
		return qr;
	}

	public static String getDIR(String path){
		File file = new File(path);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
	    DocumentBuilder builder = null;
	    NodeList qrs = null;
	    Document doc = null;
	    String qr ="";
		try {
			builder = factory.newDocumentBuilder();		
			doc = builder.parse(file);
		    qrs = doc.getElementsByTagName("name"); 
		    for(int i = 0; i < qrs.getLength(); i ++){
		    	qr += qrs.item(i).getFirstChild().getNodeValue();
		    }
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}  catch (IOException e) {
			e.printStackTrace();
		}
		return qr;
	}
	/**
	 * 根据xml文件路径和tagName获取value
	 * @author zyl
	 * @param path
	 * @param tagName
	 * @return
	 */
	public static String getValueByPathAndTagName(String path, String tagName) {
		File file = new File(path);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		NodeList qrs = null;
		Document doc = null;
		String qr = "";
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.parse(file);
			qrs = doc.getElementsByTagName(tagName);
			for (int i = 0; i < qrs.getLength(); i++) {
				qr += qrs.item(i).getFirstChild().getNodeValue();
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return qr;
	}
	
	/**
	 * 得到配置文件中的相关值
	 * 
	 * @param name
	 *            --配置文件中键名
	 * @return --返回得到的键名对应的值
	 */
	public static String readProperties(String fileName, String keyName) {
		// 得到配置文件
		InputStream in = ReadXML.class.getClassLoader().getResourceAsStream(
				fileName);
		Properties properties = new Properties();
		try {
			properties.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties.getProperty(keyName) + "";
	}
}
